/* eslint-disable vue/valid-v-else */
<template>
  <div class="my-contanier">
    <!-- 已登录模块 -->
    <div class="header login-btn" v-if="user">
      <div class="login-top">
        <div class="left">
          <van-image class="photo" round fit="cover" :src="UserInfo.photo" />
          <span class="name">{{ UserInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round size="small" class="edit-btn" to="/user/profile">编辑用户</van-button>
        </div>
      </div>
      <div class="login-bottom">
        <div class="login-box">
          <span class="count">{{ UserInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="login-box">
          <span class="count">{{ UserInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="login-box">
          <span class="count">{{ UserInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="login-box">
          <span class="count">{{ UserInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
      <!-- gird宫格导航 -->
      <van-grid :column-num="2" class="grid-nav" clickable>
        <van-grid-item class="grid-item">
          <i slot="icon" class="toutiao toutiao-shoucang"></i>
          <span slot="text" class="text">收藏</span>
        </van-grid-item>
        <van-grid-item class="grid-item">
          <i slot="icon" class="toutiao toutiao-lishi"></i>
          <span slot="text" class="text">历史</span>
        </van-grid-item>
        <!-- cell单元格 -->
        <van-cell class="cell first" title="消息通知" is-link />
        <van-cell class="cell" title="小智同学" is-link />
        <van-cell class="cell last" title="退出登录" v-if="user" @click="onLogout" />
      </van-grid>
    </div>
    <!-- 未登录模块 -->
    <div v-else class="header box">
      <div class="box-btn">
        <img class="images" src="~@/assets/mobile.png" alt="" />
        <span class="text" @click="$router.push('/login')">登录 / 注册</span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfoAPI } from '@/api/LoginAPI.js'
export default {
  name: 'MyIndex',
  data() {
    return {
      // 用户信息数据
      UserInfo: {}
    }
  },
  created() {
    // 如果用户登录，在请求用户数据
    if (this.user) {
      this.getUserInfo()
    }
  },
  methods: {
    onLogout() {
      // 退出提示
      // 在组件中使用this.$dialog来调用组件
      this.$dialog
        .confirm({
          title: '确认退出吗？'
        })
        .then(() => {
          // console.log('确认退出执行这里')
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          console.log('取消退出执行这里')
        })
    },
    // 获取用户信息数据
    async getUserInfo() {
      try {
        const { data } = await getUserInfoAPI()
        // console.log(data)
        this.UserInfo = data.data
      } catch (err) {
        this.$toast('获取信息失败')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.my-contanier {
  .header {
    height: 401px;
    background: url(~@/assets/banner.png) no-repeat;
    background-size: cover;
  }
  .box {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .box-btn {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .images {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    .text {
      font-size: 28px;
      color: #fff;
    }
  }
  .login-btn {
    .login-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 230px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .photo {
          width: 132px;
          height: 132px;
          border: 1px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
      .right {
        .edit-btn {
          width: 150px;
          height: 33px;
          color: #666;
          font-size: 20px;
        }
      }
    }
    .login-bottom {
      display: flex;
      .login-box {
        height: 130px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .count {
          font-size: 36px;
          color: #fff;
        }
        .text {
          font-size: 23px;
          color: #fff;
        }
      }
    }
  }

  .grid-nav {
    height: 140px;
    .grid-item {
      i.toutiao {
        font-size: 45px;
      }
      span.text {
        font-size: 28px;
      }
      i.toutiao-shoucang {
        color: #eb5253;
      }
      i.toutiao-lishi {
        color: #ff9d1d;
      }
    }
    .cell {
      height: 98px;
      font-size: 30px;
    }
    .first {
      margin-top: 9px;
    }
    .last {
      color: #d86262;
      text-align: center;
      margin-top: 9px;
    }
  }
}
</style>
